package com.finance.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.finance.credit.AppraisalReport;
import com.finance.dao.*;
import com.finance.loan.*;
import com.finance.mapper.*;

import com.finance.product.LoanProductInfo;
import com.finance.product.RulesInvestment;
import com.finance.service.LoanService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author ChenHm
 * @Date 2021/5/19 11:18
 */
@Service
public class LoanServiceImpl implements LoanService {

    @Resource
    private LoanDao loanDao;
    @Resource
    private agreementDao agreementDao;
    @Resource
    private recordDao recordDao;
    @Resource
    private LoanProductMapper LoanProductMapper;
    @Resource
    private accessoryLoanProductInfoMapper accessoryLoanProductInfoMapper;
    @Resource
    private accessoryDao accessoryDao;
    @Resource
    private rulesInvestmentMapper rulesInvestmentMapper;
    @Resource
    private enterpriseFinancingInfoMapper enterpriseFinancingInfoMapper;
    @Resource
    private recordAllocationDao recordAllocationDao;
    @Resource
    private companyInfoMapper companyInfoMapper;
    @Resource
    private repaymentScheduleDao repaymentScheduleDao;
    @Resource
    private  AppraisalReportMapper AppraisalReportMapper;

    @Override
    public void applyForLoan(ApplyForLoan applyForLoan) {
        //判断是否同意协议
        Integer isConsentAgreement = applyForLoan.getIsConsentAgreement();
        if (isConsentAgreement == 1) {
            AppraisalReport appraisalReport = AppraisalReportMapper.selectbyCode(applyForLoan.getBorrowerInfoCode());
            if (appraisalReport.getAmount() > applyForLoan.getLoanAmount()) {
                //1:添加申请贷款
                applyForLoan.setIsDelete(1);
                applyForLoan.setIsConsentAgreement(1);
                loanDao.save(applyForLoan);
                //2:保存协议
                Agreement agreement = applyForLoan.getAgreement();
                agreement.setTitle("贷款协议书");
                agreement.setContent("不可更改,保密协议!!!");
                agreement.setApplyForLoanCode(applyForLoan.getBorrowerInfoCode());
                agreementDao.save(agreement);
                //2:保存申请贷款记录
                RecordApplyForLoan recordApplyForLoan = new RecordApplyForLoan();
                BeanUtils.copyProperties(applyForLoan, recordApplyForLoan);
                recordDao.save(recordApplyForLoan);
                //根据选择的贷款产品找贷款产品的信息
                QueryWrapper<LoanProductInfo> loanProductInfoQueryWrapper = new QueryWrapper<>();
                loanProductInfoQueryWrapper.lambda().eq(LoanProductInfo::getName, applyForLoan.getLoanProductName());
                LoanProductInfo loanProductInfo = LoanProductMapper.selectOne(loanProductInfoQueryWrapper);
                //添加贷款产品附件信息
                AccessoryLoanProductInfo accessoryLoanProductInfo = new AccessoryLoanProductInfo();
                BeanUtils.copyProperties(loanProductInfo, accessoryLoanProductInfo);
                accessoryDao.save(accessoryLoanProductInfo);
                //分配资金
                QueryWrapper<RulesInvestment> rulesInvestmentQueryWrapper = new QueryWrapper<>();
                rulesInvestmentQueryWrapper.lambda().eq(RulesInvestment::getLoanProductCode, loanProductInfo.getCode());
                RulesInvestment rulesInvestment = rulesInvestmentMapper.selectOne(rulesInvestmentQueryWrapper);
                Integer model = rulesInvestment.getModel();
                //自营贷
                if (model == 1) {
                    //放款

                } else { //联合贷
                    //判断投资比例
                    Integer investmentProportions = rulesInvestment.getInvestmentProportions();
                    Double investmentAmount = (applyForLoan.getLoanAmount() / 10) * investmentProportions;
                    //融资公司扣钱
                    QueryWrapper<EnterpriseFinancingInfo> enterpriseFinancingInfoQueryWrapper = new QueryWrapper<>();
                    enterpriseFinancingInfoQueryWrapper.lambda().eq(EnterpriseFinancingInfo::getCompanyName, accessoryLoanProductInfo.getCompanyName());
                    EnterpriseFinancingInfo enterpriseFinancingInfo = enterpriseFinancingInfoMapper.selectOne(enterpriseFinancingInfoQueryWrapper);
                    enterpriseFinancingInfo.setFunds(enterpriseFinancingInfo.getFunds() - investmentAmount);
                    enterpriseFinancingInfoMapper.updateById(enterpriseFinancingInfo);
                    //添加分配记录
                    RecordAllocation recordAllocation = new RecordAllocation();
                    recordAllocation.setName(enterpriseFinancingInfo.getCompanyName());
                    recordAllocation.setAllocationAmount(investmentAmount);
                    recordAllocation.setApplyForLoanCode(applyForLoan.getCode());
                    recordAllocationDao.save(recordAllocation);
                    //本公司扣款
                    Double residualAmount = (applyForLoan.getLoanAmount() - investmentAmount);
                    CompanyInfo companyInfo = new CompanyInfo();
                    companyInfo.setFunds(companyInfo.getFunds() - residualAmount);
                    companyInfoMapper.updateById(companyInfo);
                    //添加分配记录
                    RecordAllocation recordAllocation1 = new RecordAllocation();
                    recordAllocation1.setName(companyInfo.getName());
                    recordAllocation1.setAllocationAmount(residualAmount);
                    recordAllocation1.setApplyForLoanCode(applyForLoan.getCode());
                    recordAllocationDao.save(recordAllocation1);
                    //生成还款计划
                    Integer loanPeriods = applyForLoan.getLoanPeriods();
                    if (loanPeriods == 1) {
                        RepaymentSchedule repaymentSchedule = new RepaymentSchedule();
                        repaymentSchedule.setIsDelete(1);
                        repaymentSchedule.setRepaymentAmount(applyForLoan.getLoanAmount());
                        repaymentSchedule.setApplyForLoanCode(applyForLoan.getCode());
                        repaymentScheduleDao.save(repaymentSchedule);
                    }
                }
            } else {
                System.out.println("额度不足!");
            }
        } else {
            System.out.println("请先同意协议");
        }
    }

}

